package com.application.lucky.activity.me;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.application.lucky.R;
import com.application.lucky.activity.PbChatActivity;
import com.application.lucky.adapter.RechargeAdapter;
import com.application.lucky.bean.UserBean;
import com.application.lucky.bean.jsonBean.RechargeGroup;
import com.application.lucky.bean.jsonBean.RechargeItem;
import com.application.lucky.config.Config;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.utils.ConnUtil;
import com.application.lucky.utils.Flog;
import com.application.lucky.widget.TitlebarView;
import com.application.lucky.widget.dialog.TwoButtonDialog;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.config.Common;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.qunar.im.base.common.ConversitionType;

import java.util.ArrayList;
import java.util.List;

public class RechargeCardActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private RecyclerView mRclView;
    private RechargeAdapter adapter;
    TwoButtonDialog twoButtonDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });

        loadData();
    }

    private void loadData() {
        new RedPRequest().red_recharge_channel(new CallBackEx<BaseResponse<List<RechargeItem>>>() {
            @Override
            public void onSuccess(BaseResponse<List<RechargeItem>> listBaseResponse) {
                if (ConnUtil.isEmpty(listBaseResponse.dataInfo)) {
                    return;
                }

                Flog.d(TAG, "red_recharge_channel:" + listBaseResponse.dataInfo);
                List<MultiItemEntity> list = new ArrayList<MultiItemEntity>();
                for (RechargeItem item : listBaseResponse.dataInfo) {

                    RechargeGroup level0 = RechargeGroup.getLevel0();
                    if (TextUtils.equals(item.getChannel(), "1")) { // 微信
                        level0.title = "微信";
                        level0.iconRes = R.drawable.icon_recharge_wx;
                        RechargeItem level1 = new RechargeItem();
                        level1.content = "微信";
                        level1.title = "微信";
                        level1.setCard(item.getCard());
                        level1.setChannel(item.getChannel());
                        level0.addSubItem(level1);
                        list.add(level0);
                    } else if (TextUtils.equals(item.getChannel(), "2")) { // 支付宝
                        level0.title = "支付宝";
                        level0.iconRes = R.drawable.icon_recharge_zfb;
                        RechargeItem level1 = new RechargeItem();
                        level1.content = "支付宝";
                        level1.title = "支付宝";
                        level1.setCard(item.getCard());
                        level1.setChannel(item.getChannel());
                        level0.addSubItem(level1);
                        list.add(level0);
                    } else if (TextUtils.equals(item.getChannel(), "3")) { // 银行卡
                        level0.title = "官方";
                        level0.iconRes = R.drawable.icon_recharge_gf;
                        RechargeItem level1 = new RechargeItem();
                        level1.content = item.getCard();
                        level1.title = "官方";
                        level1.setCard(item.getCard());
                        level1.setChannel(item.getChannel());
                        level0.addSubItem(level1);
                        list.add(level0);
                    }
                }
                adapter.setNewData(list);
            }

            @Override
            public void onFailed(int code) {

            }
        });
    }

    private View getfootView() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_recharge_foot, null);
        view.findViewById(R.id.tv_recharge_kefu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RechargeCardActivity.this, PbChatActivity.class);
                intent.putExtra(PbChatActivity.KEY_JID, Config.IMIdUtils.getIMUid(Config.KEFU_ID));
                intent.putExtra(PbChatActivity.KEY_CHAT_TYPE, String.valueOf(ConversitionType.MSG_TYPE_CHAT));
                intent.putExtra(PbChatActivity.KEY_IS_CHATROOM, false);
                startActivity(intent);
            }
        });
        return view;
    }


    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return (R.layout.activity_card_recharge);
    }


    private void initView() {
        mTitleBar = (TitlebarView) findViewById(R.id.title_bar);
        mRclView = (RecyclerView) findViewById(R.id.rcl_card_view);
        mRclView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RechargeAdapter(new ArrayList<MultiItemEntity>());
        mRclView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MultiItemEntity item1 = RechargeCardActivity.this.adapter.getItem(position);
                if (item1 instanceof RechargeItem) {
                    RechargeItem item = (RechargeItem) RechargeCardActivity.this.adapter.getItem(position);
                    if (item != null) {
                        Intent intent = new Intent(RechargeCardActivity.this, ToRechargeActivity.class);
                        intent.putExtra("channel", item.title);
                        intent.putExtra("channel_name", item.content);
                        intent.putExtra("card", item.getCard());
                        intent.putExtra("card_channel", item.getChannel());
                        startActivityAnim(intent);
                    }
                }
            }
        });
        adapter.setNewData(new ArrayList<MultiItemEntity>());
        adapter.addFooterView(getfootView());
    }

    private void checkVerify() {
        UserBean userBean = UserManagers.getInstance().getUserBean();
        if (userBean != null && UserManagers.getInstance().isTourist()) {
            twoButtonDialog = new TwoButtonDialog.Build(this)
                    .disMiss(true)
                    .setMessage("充值需要实名认证！")
                    .setRightButton("确定", new TwoButtonDialog.BaseDialogClickListener() {
                        @Override
                        public void click() {
                            twoButtonDialog.dismiss();
                            startActivityAnim(new Intent(RechargeCardActivity.this, PerfectDataActivity.class));

                        }
                    }).setLeftButton("取消", new TwoButtonDialog.BaseDialogClickListener() {
                        @Override
                        public void click() {
                            finishAnim();
                        }
                    }).create();
            twoButtonDialog.setCancelable(false);
            twoButtonDialog.setCanceledOnTouchOutside(false);
            twoButtonDialog.show();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkVerify();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (twoButtonDialog != null && twoButtonDialog.isShowing()) {
            twoButtonDialog.dismiss();
            twoButtonDialog = null;
        }
    }
}
